package com.wms.controller;

import com.mes.entity.User;
import com.wms.service.CPInWarehouseService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("CPInWarehouse")
public class CPInWarehouseController {
    @Autowired
    private CPInWarehouseService cpInWarehouseService;


    //加载入库历史
    @ResponseBody
    @RequestMapping(value = "HistoryList", method = RequestMethod.POST)
    public Message historyList(HttpServletRequest request) {
        Message msg = new Message();
        msg.setSplitPageList(cpInWarehouseService.getHistoryList());
        return msg;
    }

    //加载工单信息
    @ResponseBody
    @RequestMapping(value = "rfidLists", method = RequestMethod.POST)
    public Message rfidLists(HttpServletRequest request) {
        Message msg = new Message();
        try {
            String rfid = request.getParameter("rfid");
            if(StringUtils.isEmpty(rfid)) {
            	msg.setData("results", "false");
            	msg.setErrorMessage("[RFID]不能为空！");
            	return msg;
            }
            //判断该工单是否已完成入库及上道工序完成状态及工单
            List<Map<String, Object>> zyList = cpInWarehouseService.getjobStatusList(rfid);

            String zygxStatus = "";
            String zystatus = "";
            String exeid = "";
            if (zyList.size() > 0) {
                //判断上道工序执行状态
                zygxStatus = zyList.get(0).get("ZYGXSTATUS").toString();

                exeid = zyList.get(0).get("EXEID").toString();
                if (!zygxStatus.equals("FINISHED")) {
                    msg.setData("results", "false");
                    msg.setErrorMessage("上道工序未完工！");
                    return msg;
                }
                //判断作业工单状态
                zystatus = zyList.get(0).get("ZYSTATUS").toString();
                if (zystatus.equals("FINISHED")) {
                    msg.setData("results", "false");
                    msg.setErrorMessage("[" + rfid + "]已入库！");
                    return msg;
                }
            } else {
                msg.setData("results", "false");
                msg.setErrorMessage("[" + rfid + "]不存在！");
                return msg;
            }

            String pid = zyList.get(0).get("WLID") == null ? null : zyList.get(0).get("WLID").toString();
            String jobcode = zyList.get(0).get("ZYCODE") == null ? null : zyList.get(0).get("ZYCODE").toString();
            String jobid = zyList.get(0).get("JOBID") == null ? null : zyList.get(0).get("JOBID").toString();
            String size = zyList.get(0).get("CMID") == null ? null : zyList.get(0).get("CMID").toString();
            int qty = 1;

             String color = zyList.get(0).get("YSID") == null ? null : zyList.get(0).get("YSID").toString();
            //String color = zyList.get(0).get("YSID").toString();
            String uomid = zyList.get(0).get("DWID") == null ? null : zyList.get(0).get("DWID").toString();
            String warehouseID = "6C741F37D4DC465987187EC68D9D3CF8";
            String locatorID = null;
            // String uomID = zyList.get(0).get("WLID") == null ? "" : zyList.get(0).get("WLID").toString();
            User user = UserUtil.getUser();
           // String batchNo = GenerateCodeUtil.getCode("Wms_Storage_Batch", "Batch_No");

            cpInWarehouseService.inStorageBatch(size, color, uomid, pid, user);

            cpInWarehouseService.inStorageInOut(warehouseID, locatorID, qty, uomid, jobid, user, jobcode);

            cpInWarehouseService.uodateStatus(jobid);
            msg.setData("rfidLists", cpInWarehouseService.getjobStatusList(rfid));
            String ending ="";
            if(zygxStatus.equals("FINISHED")){
                if(zystatus.equals("CREATE")){
                    ending = "FINISHED";
                    cpInWarehouseService.UpdateExeBeginStatus(exeid,ending);
                }
            }
            msg.setData("RFID", rfid);
            msg.setData("results", "true");
            msg.setOkMessage("处理成功！");
        } catch (Exception e) {
            msg.setData("results", "false");
            msg.setErrorMessage("处理失败！");
            e.printStackTrace();
        }
        return msg;
    }

    //打印盒包装信息
    @ResponseBody
    @RequestMapping(value = "boxLists", method = RequestMethod.POST)
    public Message boxLists(HttpServletRequest request) {
        Message msg = new Message();
        String rfid = request.getParameter("rfid").toString();   //rfid
        //根据rfid查询盒包装信息
        msg.setData("Lists", cpInWarehouseService.getBoxList(rfid));
        return msg;
    }
}
	
	
	
	
